import React from "react";
import BaseComponent from "../../redare/components/BaseComponent";
import {Card, PageHeader} from "antd";
import {WebPage} from "../../redare_components/webplatform_workbench/utils/WorkbenchDecoratorUtil";
import GeneratorSysSearchForm from "../../components/generatorsys/GeneratorSysSearchForm";
import GeneratorSysGrid from "../../components/generatorsys/GeneratorSysGrid";
import GeneratorSysForm from "../../components/generatorsys/GeneratorSysForm";
import GeneratorSysPresenter from "../../.gen/presenter/GeneratorSysPresenter";


const routes = [
    {
        path: '/',
        breadcrumbName: '首页',
    },
    {
        path: '/generatorsys/manager',
        breadcrumbName: '业务系统',
    }
];

const modalName = 'generatorSysFormModal';

export default @WebPage({
    id: 'com.redare.devframework.webplatform.pages.generatorsys.GeneratorSysPage',
    name: '业务系统管理',
    webPath: '/generatorsys/manager',
    webFunIds: [
        'GeneratorSysAdd',
        'GeneratorSysEdit',
        'GeneratorSysDelete'
    ]
})
class GeneratorSysPage extends BaseComponent {

    searchForm = React.createRef();


    constructor(props) {
        super(props);
        this.presenter = new GeneratorSysPresenter(this);
    }


    componentDidMount() {
        this.registerEventListener('generatorSysAddOk', () => {
            this._onSearch();
        });
    }

    componentWillUnmount() {
        this.removeEventListener('generatorSysAddOk');
        return super.componentWillUnmount();
    }

    _onReset() {
        this.searchForm.current.resetFields()
    }

    _onSearch(values) {
        this._form.searchValues = values;
        this.grid._findGeneratorSysPage(values, 1);
    }

    _onAdd() {
        this.showModal(modalName)
    }


    render() {
        return (
            <div>
                <PageHeader className="margin-bottom bg-white" title="业务系统列表" routes={routes}/>
                <Card bordered={false} className="margin24">
                    <GeneratorSysSearchForm
                        onSearchBtnClick={this._onSearch.bind(this)} onAddBtnClick={this._onAdd.bind(this)}/>
                </Card>
                <Card bordered={false} className="margin24">
                    <GeneratorSysGrid ref={(o) => {
                        this.grid = o
                    }}
                                      itemEventListener={(event, record) => {
                                          if (!record) {
                                              this.toastFail('缺少数据记录');
                                              return;
                                          }
                                          if (event === 'edit') {
                                              this.showModal(modalName)
                                              this._form.editId = record.id
                                              this.showSpin();
                                              this.presenter.getGeneratorSys({id: record.id}, null, (result) => {
                                                      if (this.form) {
                                                          this.form.setFormData(result.data);
                                                          this.closeSpin();
                                                      }
                                                  }
                                              )
                                          }
                                      }}
                    />
                </Card>
                {
                    this.renderModal(modalName,
                        this.renderSpin(<GeneratorSysForm ref={(o) => {
                            this.form = o
                        }}/>), {
                            title: '添加任务系统',
                            okHandler: () => {
                                if (this.form) {
                                    this.form.validateFormFields().then((values) => {
                                        if (this._form.editId) {
                                            values = {id: this._urlQuery.id};
                                            this.presenter.editGeneratorSys(values, null, () => {
                                                this.grid._findGeneratorSysPage();
                                            })
                                            return;
                                        }
                                        this.presenter.addGeneratorSys(values, null, () => {
                                            this.grid._findGeneratorSysPage();
                                        })
                                    });
                                }
                            }
                        }
                    )
                }
            </div>
        )
    }


    closeLoadingIndicator() {
        this.closeSpin();
    }

}





